package com.srd.phone.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.shengrongdai.srd.R;
import com.srd.phone.mvp.presenters.UtilPresenter;
import com.srd.phone.utils.Constants;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 提现 Activity
 */
public class WithdrawActivity extends BaseActivity implements View.OnClickListener {


    private TextView tv_account_money, tv_can_money, tv_can_money_sys, tv_all_withdraw;
    private EditText et_money;
    private Button btn_withdraw;
    private UtilPresenter utilPresenter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw);

        initView();
        initData();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void initView() {
        tv_common_title.setText("提现");

        Drawable drawable = getResources().getDrawable(R.mipmap.ic_withdraw_title_right);
        drawable.setBounds(0, 0, 70, 70);
        tv_common_right.setCompoundDrawables(drawable, null, null, null);
        tv_common_right.setVisibility(View.VISIBLE);
        tv_common_right.setOnClickListener(this);

        tv_account_money = (TextView) findViewById(R.id.tv_account_money);  // 账户总额
        tv_can_money = (TextView) findViewById(R.id.tv_can_money);  // 可提取金额

        tv_can_money_sys = (TextView) findViewById(R.id.tv_can_money_sys);  // 可提现金额
        tv_all_withdraw = (TextView) findViewById(R.id.tv_all_withdraw);  // 全部提现
        tv_all_withdraw.setOnClickListener(this);

        et_money = (EditText) findViewById(R.id.et_money);  // 输入要提现的金额

        btn_withdraw = (Button) findViewById(R.id.btn_withdraw);
        btn_withdraw.setOnClickListener(this);
    }

    public void initData() {

        tv_account_money.setText(formatText("账户总额：￥0.00"));
        tv_can_money.setText(formatText("可提现金额：￥0.00"));

        utilPresenter = new UtilPresenter(requestQueue, this);
        presenter = utilPresenter;
        setUrlArrs(Constants.WITHDRAW_APP);
    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        if (Constants.WITHDRAW_APP.equals(tag)) {
            try {
                JSONObject jsonObj = new JSONObject(data);
                String url = jsonObj.optString("data");
                if(!TextUtils.isEmpty(url)){
                    Intent intent = new Intent(WithdrawActivity.this, WebViewActivity.class);
                    intent.putExtra("urlValue", url);
                    intent.putExtra("titleName", "提现");
                    gotoActivity(intent);
                    finish();
                }else{
                    toastShow("获取url地址失败~");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
        toastShow("获取url地址失败~");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tv_common_right:  // 标题右键

                break;

            case R.id.tv_all_withdraw:  // 全部提现

                break;

            case R.id.btn_withdraw:  // 确认提现

//                startActivity(new Intent(WithdrawActivity.this, WithdrawSucceedActivity.class));  // 提现成功
//                startActivity(new Intent(WithdrawActivity.this, WithdrawFailActivity.class));   // 提现失败

                String money = et_money.getText().toString().trim();
                if(TextUtils.isEmpty(money)){
                    toastShow("提现金额不能为空~");
                    return;
                }

                utilPresenter.singleParam(Constants.WITHDRAW_APP, "money", money);

                break;
        }
    }

    private SpannableStringBuilder formatText(String text){
        SpannableStringBuilder style=new SpannableStringBuilder(text);
        style.setSpan(new ForegroundColorSpan(Color.parseColor("#CB484B")), text.indexOf("￥") , text.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        return style;
    }

}
